{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# MongoDB Atlas + Fireworks AI RAG Example"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install -q llama-index llama-index-vector-stores-mongodb llama-index-embeddings-fireworks==0.1.2 llama-index-llms-fireworks\n",
    "!pip install -q pymongo datasets pandas"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# set up Fireworks.ai Key\n",
    "import os\n",
    "import getpass\n",
    "\n",
    "fw_api_key = getpass.getpass(\"Fireworks API Key:\")\n",
    "os.environ[\"FIREWORKS_API_KEY\"] = fw_api_key"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/mnt/disks/data/llama_index/.venv/lib/python3.10/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
      "  from .autonotebook import tqdm as notebook_tqdm\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>restaurant_id</th>\n",
       "      <th>attributes</th>\n",
       "      <th>cuisine</th>\n",
       "      <th>DogsAllowed</th>\n",
       "      <th>embedding</th>\n",
       "      <th>OutdoorSeating</th>\n",
       "      <th>borough</th>\n",
       "      <th>address</th>\n",
       "      <th>_id</th>\n",
       "      <th>name</th>\n",
       "      <th>menu</th>\n",
       "      <th>TakeOut</th>\n",
       "      <th>location</th>\n",
       "      <th>PriceRange</th>\n",
       "      <th>HappyHour</th>\n",
       "      <th>review_count</th>\n",
       "      <th>sponsored</th>\n",
       "      <th>stars</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>40366661</td>\n",
       "      <td>{'Alcohol': ''none'', 'Ambience': '{'romantic'...</td>\n",
       "      <td>Tex-Mex</td>\n",
       "      <td>None</td>\n",
       "      <td>[-0.14520384, 0.018315623, -0.018330636, -0.10...</td>\n",
       "      <td>True</td>\n",
       "      <td>Manhattan</td>\n",
       "      <td>{'building': '627', 'coord': [-73.975980999999...</td>\n",
       "      <td>{'$oid': '6095a34a7c34416a90d3206b'}</td>\n",
       "      <td>Baby Bo'S Burritos</td>\n",
       "      <td>None</td>\n",
       "      <td>True</td>\n",
       "      <td>{'coordinates': [-73.97598099999999, 40.745132...</td>\n",
       "      <td>1.0</td>\n",
       "      <td>None</td>\n",
       "      <td>10</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>40367442</td>\n",
       "      <td>{'Alcohol': ''beer_and_wine'', 'Ambience': '{'...</td>\n",
       "      <td>American</td>\n",
       "      <td>True</td>\n",
       "      <td>[-0.11977468, -0.02157107, 0.0038846824, -0.09...</td>\n",
       "      <td>True</td>\n",
       "      <td>Staten Island</td>\n",
       "      <td>{'building': '17', 'coord': [-74.1350211, 40.6...</td>\n",
       "      <td>{'$oid': '6095a34a7c34416a90d3209e'}</td>\n",
       "      <td>Buddy'S Wonder Bar</td>\n",
       "      <td>[Grilled cheese sandwich, Baked potato, Lasagn...</td>\n",
       "      <td>True</td>\n",
       "      <td>{'coordinates': [-74.1350211, 40.6369042], 'ty...</td>\n",
       "      <td>2.0</td>\n",
       "      <td>None</td>\n",
       "      <td>62</td>\n",
       "      <td>NaN</td>\n",
       "      <td>3.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>40364610</td>\n",
       "      <td>{'Alcohol': ''none'', 'Ambience': '{'touristy'...</td>\n",
       "      <td>American</td>\n",
       "      <td>None</td>\n",
       "      <td>[-0.1004329, -0.014882699, -0.033005167, -0.09...</td>\n",
       "      <td>True</td>\n",
       "      <td>Staten Island</td>\n",
       "      <td>{'building': '37', 'coord': [-74.138263, 40.54...</td>\n",
       "      <td>{'$oid': '6095a34a7c34416a90d31ff6'}</td>\n",
       "      <td>Great Kills Yacht Club</td>\n",
       "      <td>[Mozzarella sticks, Mushroom swiss burger, Spi...</td>\n",
       "      <td>True</td>\n",
       "      <td>{'coordinates': [-74.138263, 40.546681], 'type...</td>\n",
       "      <td>1.0</td>\n",
       "      <td>None</td>\n",
       "      <td>72</td>\n",
       "      <td>NaN</td>\n",
       "      <td>4.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>40365288</td>\n",
       "      <td>{'Alcohol': None, 'Ambience': '{'touristy': Fa...</td>\n",
       "      <td>American</td>\n",
       "      <td>None</td>\n",
       "      <td>[-0.11735515, -0.0397448, -0.0072645755, -0.09...</td>\n",
       "      <td>True</td>\n",
       "      <td>Manhattan</td>\n",
       "      <td>{'building': '842', 'coord': [-73.970637000000...</td>\n",
       "      <td>{'$oid': '6095a34a7c34416a90d32017'}</td>\n",
       "      <td>Keats Restaurant</td>\n",
       "      <td>[French fries, Chicken pot pie, Mac &amp; cheese, ...</td>\n",
       "      <td>True</td>\n",
       "      <td>{'coordinates': [-73.97063700000001, 40.751495...</td>\n",
       "      <td>2.0</td>\n",
       "      <td>True</td>\n",
       "      <td>149</td>\n",
       "      <td>NaN</td>\n",
       "      <td>4.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>40363151</td>\n",
       "      <td>{'Alcohol': None, 'Ambience': None, 'BYOB': No...</td>\n",
       "      <td>Bakery</td>\n",
       "      <td>None</td>\n",
       "      <td>[-0.096541286, -0.009661355, 0.04402167, -0.12...</td>\n",
       "      <td>True</td>\n",
       "      <td>Manhattan</td>\n",
       "      <td>{'building': '120', 'coord': [-73.9998042, 40....</td>\n",
       "      <td>{'$oid': '6095a34a7c34416a90d31fbd'}</td>\n",
       "      <td>Olive'S</td>\n",
       "      <td>[doughnuts, chocolate chip cookies, chocolate ...</td>\n",
       "      <td>True</td>\n",
       "      <td>{'coordinates': [-73.9998042, 40.7251256], 'ty...</td>\n",
       "      <td>1.0</td>\n",
       "      <td>None</td>\n",
       "      <td>7</td>\n",
       "      <td>NaN</td>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  restaurant_id                                         attributes   cuisine  \\\n",
       "0      40366661  {'Alcohol': ''none'', 'Ambience': '{'romantic'...   Tex-Mex   \n",
       "1      40367442  {'Alcohol': ''beer_and_wine'', 'Ambience': '{'...  American   \n",
       "2      40364610  {'Alcohol': ''none'', 'Ambience': '{'touristy'...  American   \n",
       "3      40365288  {'Alcohol': None, 'Ambience': '{'touristy': Fa...  American   \n",
       "4      40363151  {'Alcohol': None, 'Ambience': None, 'BYOB': No...    Bakery   \n",
       "\n",
       "  DogsAllowed                                          embedding  \\\n",
       "0        None  [-0.14520384, 0.018315623, -0.018330636, -0.10...   \n",
       "1        True  [-0.11977468, -0.02157107, 0.0038846824, -0.09...   \n",
       "2        None  [-0.1004329, -0.014882699, -0.033005167, -0.09...   \n",
       "3        None  [-0.11735515, -0.0397448, -0.0072645755, -0.09...   \n",
       "4        None  [-0.096541286, -0.009661355, 0.04402167, -0.12...   \n",
       "\n",
       "  OutdoorSeating        borough  \\\n",
       "0           True      Manhattan   \n",
       "1           True  Staten Island   \n",
       "2           True  Staten Island   \n",
       "3           True      Manhattan   \n",
       "4           True      Manhattan   \n",
       "\n",
       "                                             address  \\\n",
       "0  {'building': '627', 'coord': [-73.975980999999...   \n",
       "1  {'building': '17', 'coord': [-74.1350211, 40.6...   \n",
       "2  {'building': '37', 'coord': [-74.138263, 40.54...   \n",
       "3  {'building': '842', 'coord': [-73.970637000000...   \n",
       "4  {'building': '120', 'coord': [-73.9998042, 40....   \n",
       "\n",
       "                                    _id                    name  \\\n",
       "0  {'$oid': '6095a34a7c34416a90d3206b'}      Baby Bo'S Burritos   \n",
       "1  {'$oid': '6095a34a7c34416a90d3209e'}      Buddy'S Wonder Bar   \n",
       "2  {'$oid': '6095a34a7c34416a90d31ff6'}  Great Kills Yacht Club   \n",
       "3  {'$oid': '6095a34a7c34416a90d32017'}        Keats Restaurant   \n",
       "4  {'$oid': '6095a34a7c34416a90d31fbd'}                 Olive'S   \n",
       "\n",
       "                                                menu  TakeOut  \\\n",
       "0                                               None     True   \n",
       "1  [Grilled cheese sandwich, Baked potato, Lasagn...     True   \n",
       "2  [Mozzarella sticks, Mushroom swiss burger, Spi...     True   \n",
       "3  [French fries, Chicken pot pie, Mac & cheese, ...     True   \n",
       "4  [doughnuts, chocolate chip cookies, chocolate ...     True   \n",
       "\n",
       "                                            location  PriceRange HappyHour  \\\n",
       "0  {'coordinates': [-73.97598099999999, 40.745132...         1.0      None   \n",
       "1  {'coordinates': [-74.1350211, 40.6369042], 'ty...         2.0      None   \n",
       "2  {'coordinates': [-74.138263, 40.546681], 'type...         1.0      None   \n",
       "3  {'coordinates': [-73.97063700000001, 40.751495...         2.0      True   \n",
       "4  {'coordinates': [-73.9998042, 40.7251256], 'ty...         1.0      None   \n",
       "\n",
       "   review_count  sponsored  stars  \n",
       "0            10        NaN    2.5  \n",
       "1            62        NaN    3.5  \n",
       "2            72        NaN    4.0  \n",
       "3           149        NaN    4.0  \n",
       "4             7        NaN    5.0  "
      ]
     },
     "execution_count": null,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from datasets import load_dataset\n",
    "import pandas as pd\n",
    "\n",
    "# https://huggingface.co/datasets/AIatMongoDB/whatscooking.restaurants\n",
    "dataset = load_dataset(\"AIatMongoDB/whatscooking.restaurants\")\n",
    "\n",
    "# Convert the dataset to a pandas dataframe\n",
    "dataset_df = pd.DataFrame(dataset[\"train\"])\n",
    "\n",
    "dataset_df.head(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from llama_index.core.settings import Settings\n",
    "from llama_index.llms.fireworks import Fireworks\n",
    "from llama_index.embeddings.fireworks import FireworksEmbedding\n",
    "\n",
    "embed_model = FireworksEmbedding(\n",
    "    embed_batch_size=512,\n",
    "    model_name=\"nomic-ai/nomic-embed-text-v1.5\",\n",
    "    api_key=fw_api_key,\n",
    ")\n",
    "llm = Fireworks(\n",
    "    temperature=0,\n",
    "    model=\"accounts/fireworks/models/mixtral-8x7b-instruct\",\n",
    "    api_key=fw_api_key,\n",
    ")\n",
    "\n",
    "Settings.llm = llm\n",
    "Settings.embed_model = embed_model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "The LLM sees this: \n",
      " Metadata: restaurant_id=>40366661\n",
      "attributes=>{\"Alcohol\": \"'none'\", \"Ambience\": \"{'romantic': False, 'intimate': False, 'classy': False, 'hipster': False, 'divey': False, 'touristy': False, 'trendy': False, 'upscale': False, 'casual': False}\", \"BYOB\": null, \"BestNights\": null, \"BikeParking\": null, \"BusinessAcceptsBitcoin\": null, \"BusinessAcceptsCreditCards\": null, \"BusinessParking\": \"None\", \"Caters\": \"True\", \"DriveThru\": null, \"GoodForDancing\": null, \"GoodForKids\": \"True\", \"GoodForMeal\": null, \"HasTV\": \"True\", \"Music\": null, \"NoiseLevel\": \"'average'\", \"RestaurantsAttire\": \"'casual'\", \"RestaurantsDelivery\": \"True\", \"RestaurantsGoodForGroups\": \"True\", \"RestaurantsReservations\": \"True\", \"RestaurantsTableService\": \"False\", \"WheelchairAccessible\": \"True\", \"WiFi\": \"'free'\"}\n",
      "cuisine=>\"Tex-Mex\"\n",
      "DogsAllowed=>None\n",
      "OutdoorSeating=>True\n",
      "borough=>\"Manhattan\"\n",
      "address=>{\"building\": \"627\", \"coord\": [-73.975981, 40.745132], \"street\": \"2 Avenue\", \"zipcode\": \"10016\"}\n",
      "_id=>{'$oid': '6095a34a7c34416a90d3206b'}\n",
      "name=>\"Baby Bo'S Burritos\"\n",
      "menu=>null\n",
      "TakeOut=>true\n",
      "PriceRange=>1.0\n",
      "HappyHour=>null\n",
      "review_count=>10\n",
      "sponsored=>None\n",
      "stars=>2.5\n",
      "-----\n",
      "Content: {\"restaurant_id\": \"40366661\", \"attributes\": \"{\\\"Alcohol\\\": \\\"'none'\\\", \\\"Ambience\\\": \\\"{'romantic': False, 'intimate': False, 'classy': False, 'hipster': False, 'divey': False, 'touristy': False, 'trendy': False, 'upscale': False, 'casual': False}\\\", \\\"BYOB\\\": null, \\\"BestNights\\\": null, \\\"BikeParking\\\": null, \\\"BusinessAcceptsBitcoin\\\": null, \\\"BusinessAcceptsCreditCards\\\": null, \\\"BusinessParking\\\": \\\"None\\\", \\\"Caters\\\": \\\"True\\\", \\\"DriveThru\\\": null, \\\"GoodForDancing\\\": null, \\\"GoodForKids\\\": \\\"True\\\", \\\"GoodForMeal\\\": null, \\\"HasTV\\\": \\\"True\\\", \\\"Music\\\": null, \\\"NoiseLevel\\\": \\\"'average'\\\", \\\"RestaurantsAttire\\\": \\\"'casual'\\\", \\\"RestaurantsDelivery\\\": \\\"True\\\", \\\"RestaurantsGoodForGroups\\\": \\\"True\\\", \\\"RestaurantsReservations\\\": \\\"True\\\", \\\"RestaurantsTableService\\\": \\\"False\\\", \\\"WheelchairAccessible\\\": \\\"True\\\", \\\"WiFi\\\": \\\"'free'\\\"}\", \"cuisine\": \"\\\"Tex-Mex\\\"\", \"DogsAllowed\": null, \"OutdoorSeating\": true, \"borough\": \"\\\"Manhattan\\\"\", \"address\": \"{\\\"building\\\": \\\"627\\\", \\\"coord\\\": [-73.975981, 40.745132], \\\"street\\\": \\\"2 Avenue\\\", \\\"zipcode\\\": \\\"10016\\\"}\", \"_id\": {\"$oid\": \"6095a34a7c34416a90d3206b\"}, \"name\": \"\\\"Baby Bo'S Burritos\\\"\", \"menu\": \"null\", \"TakeOut\": \"true\", \"PriceRange\": \"1.0\", \"HappyHour\": \"null\", \"review_count\": \"10\", \"sponsored\": null, \"stars\": 2.5}\n",
      "\n",
      "The Embedding model sees this: \n",
      " Metadata: restaurant_id=>40366661\n",
      "attributes=>{\"Alcohol\": \"'none'\", \"Ambience\": \"{'romantic': False, 'intimate': False, 'classy': False, 'hipster': False, 'divey': False, 'touristy': False, 'trendy': False, 'upscale': False, 'casual': False}\", \"BYOB\": null, \"BestNights\": null, \"BikeParking\": null, \"BusinessAcceptsBitcoin\": null, \"BusinessAcceptsCreditCards\": null, \"BusinessParking\": \"None\", \"Caters\": \"True\", \"DriveThru\": null, \"GoodForDancing\": null, \"GoodForKids\": \"True\", \"GoodForMeal\": null, \"HasTV\": \"True\", \"Music\": null, \"NoiseLevel\": \"'average'\", \"RestaurantsAttire\": \"'casual'\", \"RestaurantsDelivery\": \"True\", \"RestaurantsGoodForGroups\": \"True\", \"RestaurantsReservations\": \"True\", \"RestaurantsTableService\": \"False\", \"WheelchairAccessible\": \"True\", \"WiFi\": \"'free'\"}\n",
      "cuisine=>\"Tex-Mex\"\n",
      "DogsAllowed=>None\n",
      "OutdoorSeating=>True\n",
      "borough=>\"Manhattan\"\n",
      "address=>{\"building\": \"627\", \"coord\": [-73.975981, 40.745132], \"street\": \"2 Avenue\", \"zipcode\": \"10016\"}\n",
      "_id=>{'$oid': '6095a34a7c34416a90d3206b'}\n",
      "name=>\"Baby Bo'S Burritos\"\n",
      "menu=>null\n",
      "TakeOut=>true\n",
      "PriceRange=>1.0\n",
      "HappyHour=>null\n",
      "review_count=>10\n",
      "sponsored=>None\n",
      "stars=>2.5\n",
      "-----\n",
      "Content: {\"restaurant_id\": \"40366661\", \"attributes\": \"{\\\"Alcohol\\\": \\\"'none'\\\", \\\"Ambience\\\": \\\"{'romantic': False, 'intimate': False, 'classy': False, 'hipster': False, 'divey': False, 'touristy': False, 'trendy': False, 'upscale': False, 'casual': False}\\\", \\\"BYOB\\\": null, \\\"BestNights\\\": null, \\\"BikeParking\\\": null, \\\"BusinessAcceptsBitcoin\\\": null, \\\"BusinessAcceptsCreditCards\\\": null, \\\"BusinessParking\\\": \\\"None\\\", \\\"Caters\\\": \\\"True\\\", \\\"DriveThru\\\": null, \\\"GoodForDancing\\\": null, \\\"GoodForKids\\\": \\\"True\\\", \\\"GoodForMeal\\\": null, \\\"HasTV\\\": \\\"True\\\", \\\"Music\\\": null, \\\"NoiseLevel\\\": \\\"'average'\\\", \\\"RestaurantsAttire\\\": \\\"'casual'\\\", \\\"RestaurantsDelivery\\\": \\\"True\\\", \\\"RestaurantsGoodForGroups\\\": \\\"True\\\", \\\"RestaurantsReservations\\\": \\\"True\\\", \\\"RestaurantsTableService\\\": \\\"False\\\", \\\"WheelchairAccessible\\\": \\\"True\\\", \\\"WiFi\\\": \\\"'free'\\\"}\", \"cuisine\": \"\\\"Tex-Mex\\\"\", \"DogsAllowed\": null, \"OutdoorSeating\": true, \"borough\": \"\\\"Manhattan\\\"\", \"address\": \"{\\\"building\\\": \\\"627\\\", \\\"coord\\\": [-73.975981, 40.745132], \\\"street\\\": \\\"2 Avenue\\\", \\\"zipcode\\\": \\\"10016\\\"}\", \"_id\": {\"$oid\": \"6095a34a7c34416a90d3206b\"}, \"name\": \"\\\"Baby Bo'S Burritos\\\"\", \"menu\": \"null\", \"TakeOut\": \"true\", \"PriceRange\": \"1.0\", \"HappyHour\": \"null\", \"review_count\": \"10\", \"sponsored\": null, \"stars\": 2.5}\n"
     ]
    }
   ],
   "source": [
    "import json\n",
    "from llama_index.core import Document\n",
    "from llama_index.core.schema import MetadataMode\n",
    "\n",
    "# Convert the DataFrame to a JSON string representation\n",
    "documents_json = dataset_df.to_json(orient=\"records\")\n",
    "# Load the JSON string into a Python list of dictionaries\n",
    "documents_list = json.loads(documents_json)\n",
    "\n",
    "llama_documents = []\n",
    "\n",
    "for document in documents_list:\n",
    "    # Value for metadata must be one of (str, int, float, None)\n",
    "    document[\"name\"] = json.dumps(document[\"name\"])\n",
    "    document[\"cuisine\"] = json.dumps(document[\"cuisine\"])\n",
    "    document[\"attributes\"] = json.dumps(document[\"attributes\"])\n",
    "    document[\"menu\"] = json.dumps(document[\"menu\"])\n",
    "    document[\"borough\"] = json.dumps(document[\"borough\"])\n",
    "    document[\"address\"] = json.dumps(document[\"address\"])\n",
    "    document[\"PriceRange\"] = json.dumps(document[\"PriceRange\"])\n",
    "    document[\"HappyHour\"] = json.dumps(document[\"HappyHour\"])\n",
    "    document[\"review_count\"] = json.dumps(document[\"review_count\"])\n",
    "    document[\"TakeOut\"] = json.dumps(document[\"TakeOut\"])\n",
    "    # these two fields are not relevant to the question we want to answer,\n",
    "    # so I will skip it for now\n",
    "    del document[\"embedding\"]\n",
    "    del document[\"location\"]\n",
    "\n",
    "    # Create a Document object with the text and excluded metadata for llm and embedding models\n",
    "    llama_document = Document(\n",
    "        text=json.dumps(document),\n",
    "        metadata=document,\n",
    "        metadata_template=\"{key}=>{value}\",\n",
    "        text_template=\"Metadata: {metadata_str}\\n-----\\nContent: {content}\",\n",
    "    )\n",
    "\n",
    "    llama_documents.append(llama_document)\n",
    "\n",
    "# Observing an example of what the LLM and Embedding model receive as input\n",
    "print(\n",
    "    \"\\nThe LLM sees this: \\n\",\n",
    "    llama_documents[0].get_content(metadata_mode=MetadataMode.LLM),\n",
    ")\n",
    "print(\n",
    "    \"\\nThe Embedding model sees this: \\n\",\n",
    "    llama_documents[0].get_content(metadata_mode=MetadataMode.EMBED),\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Document(id_='93d3f08d-85f3-494d-a057-19bc834abc29', embedding=None, metadata={'restaurant_id': '40366661', 'attributes': '{\"Alcohol\": \"\\'none\\'\", \"Ambience\": \"{\\'romantic\\': False, \\'intimate\\': False, \\'classy\\': False, \\'hipster\\': False, \\'divey\\': False, \\'touristy\\': False, \\'trendy\\': False, \\'upscale\\': False, \\'casual\\': False}\", \"BYOB\": null, \"BestNights\": null, \"BikeParking\": null, \"BusinessAcceptsBitcoin\": null, \"BusinessAcceptsCreditCards\": null, \"BusinessParking\": \"None\", \"Caters\": \"True\", \"DriveThru\": null, \"GoodForDancing\": null, \"GoodForKids\": \"True\", \"GoodForMeal\": null, \"HasTV\": \"True\", \"Music\": null, \"NoiseLevel\": \"\\'average\\'\", \"RestaurantsAttire\": \"\\'casual\\'\", \"RestaurantsDelivery\": \"True\", \"RestaurantsGoodForGroups\": \"True\", \"RestaurantsReservations\": \"True\", \"RestaurantsTableService\": \"False\", \"WheelchairAccessible\": \"True\", \"WiFi\": \"\\'free\\'\"}', 'cuisine': '\"Tex-Mex\"', 'DogsAllowed': None, 'OutdoorSeating': True, 'borough': '\"Manhattan\"', 'address': '{\"building\": \"627\", \"coord\": [-73.975981, 40.745132], \"street\": \"2 Avenue\", \"zipcode\": \"10016\"}', '_id': {'$oid': '6095a34a7c34416a90d3206b'}, 'name': '\"Baby Bo\\'S Burritos\"', 'menu': 'null', 'TakeOut': 'true', 'PriceRange': '1.0', 'HappyHour': 'null', 'review_count': '10', 'sponsored': None, 'stars': 2.5}, excluded_embed_metadata_keys=[], excluded_llm_metadata_keys=[], relationships={}, text='{\"restaurant_id\": \"40366661\", \"attributes\": \"{\\\\\"Alcohol\\\\\": \\\\\"\\'none\\'\\\\\", \\\\\"Ambience\\\\\": \\\\\"{\\'romantic\\': False, \\'intimate\\': False, \\'classy\\': False, \\'hipster\\': False, \\'divey\\': False, \\'touristy\\': False, \\'trendy\\': False, \\'upscale\\': False, \\'casual\\': False}\\\\\", \\\\\"BYOB\\\\\": null, \\\\\"BestNights\\\\\": null, \\\\\"BikeParking\\\\\": null, \\\\\"BusinessAcceptsBitcoin\\\\\": null, \\\\\"BusinessAcceptsCreditCards\\\\\": null, \\\\\"BusinessParking\\\\\": \\\\\"None\\\\\", \\\\\"Caters\\\\\": \\\\\"True\\\\\", \\\\\"DriveThru\\\\\": null, \\\\\"GoodForDancing\\\\\": null, \\\\\"GoodForKids\\\\\": \\\\\"True\\\\\", \\\\\"GoodForMeal\\\\\": null, \\\\\"HasTV\\\\\": \\\\\"True\\\\\", \\\\\"Music\\\\\": null, \\\\\"NoiseLevel\\\\\": \\\\\"\\'average\\'\\\\\", \\\\\"RestaurantsAttire\\\\\": \\\\\"\\'casual\\'\\\\\", \\\\\"RestaurantsDelivery\\\\\": \\\\\"True\\\\\", \\\\\"RestaurantsGoodForGroups\\\\\": \\\\\"True\\\\\", \\\\\"RestaurantsReservations\\\\\": \\\\\"True\\\\\", \\\\\"RestaurantsTableService\\\\\": \\\\\"False\\\\\", \\\\\"WheelchairAccessible\\\\\": \\\\\"True\\\\\", \\\\\"WiFi\\\\\": \\\\\"\\'free\\'\\\\\"}\", \"cuisine\": \"\\\\\"Tex-Mex\\\\\"\", \"DogsAllowed\": null, \"OutdoorSeating\": true, \"borough\": \"\\\\\"Manhattan\\\\\"\", \"address\": \"{\\\\\"building\\\\\": \\\\\"627\\\\\", \\\\\"coord\\\\\": [-73.975981, 40.745132], \\\\\"street\\\\\": \\\\\"2 Avenue\\\\\", \\\\\"zipcode\\\\\": \\\\\"10016\\\\\"}\", \"_id\": {\"$oid\": \"6095a34a7c34416a90d3206b\"}, \"name\": \"\\\\\"Baby Bo\\'S Burritos\\\\\"\", \"menu\": \"null\", \"TakeOut\": \"true\", \"PriceRange\": \"1.0\", \"HappyHour\": \"null\", \"review_count\": \"10\", \"sponsored\": null, \"stars\": 2.5}', start_char_idx=None, end_char_idx=None, text_template='Metadata: {metadata_str}\\n-----\\nContent: {content}', metadata_template='{key}=>{value}', metadata_seperator='\\n')"
      ]
     },
     "execution_count": null,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "llama_documents[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from llama_index.core.node_parser import SentenceSplitter\n",
    "\n",
    "parser = SentenceSplitter()\n",
    "nodes = parser.get_nodes_from_documents(llama_documents)\n",
    "# 25k nodes takes about 10 minutes, will trim it down to 2.5k\n",
    "new_nodes = nodes[:2500]\n",
    "\n",
    "# There are 25k documents, so we need to do batching. Fortunately LlamaIndex provides good batching\n",
    "# for embedding models, and we are going to rely on the __call__ method for the model to handle this\n",
    "node_embeddings = embed_model(new_nodes)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for idx, n in enumerate(new_nodes):\n",
    "    n.embedding = node_embeddings[idx].embedding\n",
    "    if \"_id\" in n.metadata:\n",
    "        del n.metadata[\"_id\"]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Ensure your database, collection and vector store index is setup on MongoDB Atlas for the collection or the following step won't work appropriately on MongoDB.\n",
    "\n",
    "\n",
    " - For assistance with database cluster setup and obtaining the URI, refer to this [guide](https://www.mongodb.com/docs/guides/atlas/cluster/) for setting up a MongoDB cluster, and this [guide](https://www.mongodb.com/docs/guides/atlas/connection-string/) to get your connection string. \n",
    "\n",
    " - Once you have successfully created a cluster, create the database and collection within the MongoDB Atlas cluster by clicking “+ Create Database”. The database will be named movies, and the collection will be named movies_records.\n",
    "\n",
    " - Creating a vector search index within the movies_records collection is essential for efficient document retrieval from MongoDB into our development environment. To achieve this, refer to the official [guide](https://www.mongodb.com/docs/atlas/atlas-vector-search/create-index/) on vector search index creation.\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Connection to MongoDB successful\n"
     ]
    }
   ],
   "source": [
    "import pymongo\n",
    "\n",
    "\n",
    "def get_mongo_client(mongo_uri):\n",
    "    \"\"\"Establish connection to the MongoDB.\"\"\"\n",
    "    try:\n",
    "        client = pymongo.MongoClient(mongo_uri)\n",
    "        print(\"Connection to MongoDB successful\")\n",
    "        return client\n",
    "    except pymongo.errors.ConnectionFailure as e:\n",
    "        print(f\"Connection failed: {e}\")\n",
    "        return None\n",
    "\n",
    "\n",
    "# set up Fireworks.ai Key\n",
    "import os\n",
    "import getpass\n",
    "\n",
    "mongo_uri = getpass.getpass(\"MONGO_URI:\")\n",
    "if not mongo_uri:\n",
    "    print(\"MONGO_URI not set\")\n",
    "\n",
    "mongo_client = get_mongo_client(mongo_uri)\n",
    "\n",
    "DB_NAME = \"whatscooking\"\n",
    "COLLECTION_NAME = \"restaurants\"\n",
    "\n",
    "db = mongo_client[DB_NAME]\n",
    "collection = db[COLLECTION_NAME]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DeleteResult({'n': 0, 'electionId': ObjectId('7fffffff00000000000001ce'), 'opTime': {'ts': Timestamp(1708970193, 3), 't': 462}, 'ok': 1.0, '$clusterTime': {'clusterTime': Timestamp(1708970193, 3), 'signature': {'hash': b'\\x9a3H8\\xa1\\x1b\\xb6\\xbb\\xa9\\xc3x\\x17\\x1c\\xeb\\xe9\\x03\\xaa\\xf8\\xf17', 'keyId': 7294687148333072386}}, 'operationTime': Timestamp(1708970193, 3)}, acknowledged=True)"
      ]
     },
     "execution_count": null,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# To ensure we are working with a fresh collection\n",
    "# delete any existing records in the collection\n",
    "collection.delete_many({})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from llama_index.vector_stores.mongodb import MongoDBAtlasVectorSearch\n",
    "\n",
    "vector_store = MongoDBAtlasVectorSearch(\n",
    "    mongo_client,\n",
    "    db_name=DB_NAME,\n",
    "    collection_name=COLLECTION_NAME,\n",
    "    index_name=\"vector_index\",\n",
    ")\n",
    "vector_store.add(new_nodes)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "now make sure you create the search index with the right name here"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from llama_index.core import VectorStoreIndex, StorageContext\n",
    "\n",
    "index = VectorStoreIndex.from_vector_store(vector_store)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Note: you may need to restart the kernel to use updated packages.\n"
     ]
    }
   ],
   "source": [
    "%pip install -q matplotlib"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/markdown": [
       "**`Final Response:`** Based on the context provided, two restaurant options that don't serve alcohol are:\n",
       "\n",
       "1. \"Academy Restauraunt\" in Brooklyn, which serves American cuisine and has a variety of dishes such as Mozzarella sticks, Cheeseburger, Baked potato, Breadsticks, Caesar salad, Chicken parmesan, Pigs in a blanket, Chicken soup, Mac & cheese, Mushroom swiss burger, Spaghetti with meatballs, and Mashed potatoes.\n",
       "\n",
       "2. \"Gabriel'S Bar & Grill\" in Manhattan, which specializes in Italian cuisine and offers dishes like Cheese Ravioli, Neapolitan Pizza, assorted gelato, Vegetarian Baked Ziti, Vegetarian Broccoli Pizza, Lasagna, Buca Trio Platter, Spinach Ravioli, Pasta with ricotta cheese, Spaghetti, Fried calamari, and Alfredo Pizza.\n",
       "\n",
       "Both restaurants offer outdoor seating, are kid-friendly, and have a casual dress code. They also provide take-out service and have happy hour promotions."
      ],
      "text/plain": [
       "<IPython.core.display.Markdown object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[NodeWithScore(node=TextNode(id_='5405e68c-19f2-4a65-95d7-f880fa6a8deb', embedding=None, metadata={'restaurant_id': '40385767', 'attributes': '{\"Alcohol\": \"u\\'beer_and_wine\\'\", \"Ambience\": \"{\\'touristy\\': False, \\'hipster\\': False, \\'romantic\\': False, \\'divey\\': False, \\'intimate\\': None, \\'trendy\\': None, \\'upscale\\': False, \\'classy\\': False, \\'casual\\': True}\", \"BYOB\": null, \"BestNights\": \"{\\'monday\\': False, \\'tuesday\\': False, \\'friday\\': True, \\'wednesday\\': False, \\'thursday\\': False, \\'sunday\\': False, \\'saturday\\': True}\", \"BikeParking\": \"True\", \"BusinessAcceptsBitcoin\": \"False\", \"BusinessAcceptsCreditCards\": \"True\", \"BusinessParking\": \"{\\'garage\\': False, \\'street\\': False, \\'validated\\': False, \\'lot\\': True, \\'valet\\': False}\", \"Caters\": \"True\", \"DriveThru\": null, \"GoodForDancing\": \"False\", \"GoodForKids\": \"True\", \"GoodForMeal\": \"{\\'dessert\\': False, \\'latenight\\': False, \\'lunch\\': True, \\'dinner\\': True, \\'brunch\\': False, \\'breakfast\\': False}\", \"HasTV\": \"True\", \"Music\": \"{\\'dj\\': False, \\'background_music\\': False, \\'no_music\\': False, \\'jukebox\\': False, \\'live\\': False, \\'video\\': False, \\'karaoke\\': False}\", \"NoiseLevel\": \"u\\'average\\'\", \"RestaurantsAttire\": \"u\\'casual\\'\", \"RestaurantsDelivery\": \"None\", \"RestaurantsGoodForGroups\": \"True\", \"RestaurantsReservations\": \"True\", \"RestaurantsTableService\": \"True\", \"WheelchairAccessible\": \"True\", \"WiFi\": \"u\\'free\\'\"}', 'cuisine': '\"American\"', 'DogsAllowed': True, 'OutdoorSeating': True, 'borough': '\"Brooklyn\"', 'address': '{\"building\": \"69\", \"coord\": [-73.9757464, 40.687295], \"street\": \"Lafayette Avenue\", \"zipcode\": \"11217\"}', 'name': '\"Academy Restauraunt\"', 'menu': '[\"Mozzarella sticks\", \"Cheeseburger\", \"Baked potato\", \"Breadsticks\", \"Caesar salad\", \"Chicken parmesan\", \"Pigs in a blanket\", \"Chicken soup\", \"Mac & cheese\", \"Mushroom swiss burger\", \"Spaghetti with meatballs\", \"Mashed potatoes\"]', 'TakeOut': 'true', 'PriceRange': '2.0', 'HappyHour': 'true', 'review_count': '173', 'sponsored': None, 'stars': 4.5}, excluded_embed_metadata_keys=[], excluded_llm_metadata_keys=[], relationships={<NodeRelationship.SOURCE: '1'>: RelatedNodeInfo(node_id='bbfc4bf5-d9c3-4f3b-8c1f-ddcf94f3b5df', node_type=<ObjectType.DOCUMENT: '4'>, metadata={'restaurant_id': '40385767', 'attributes': '{\"Alcohol\": \"u\\'beer_and_wine\\'\", \"Ambience\": \"{\\'touristy\\': False, \\'hipster\\': False, \\'romantic\\': False, \\'divey\\': False, \\'intimate\\': None, \\'trendy\\': None, \\'upscale\\': False, \\'classy\\': False, \\'casual\\': True}\", \"BYOB\": null, \"BestNights\": \"{\\'monday\\': False, \\'tuesday\\': False, \\'friday\\': True, \\'wednesday\\': False, \\'thursday\\': False, \\'sunday\\': False, \\'saturday\\': True}\", \"BikeParking\": \"True\", \"BusinessAcceptsBitcoin\": \"False\", \"BusinessAcceptsCreditCards\": \"True\", \"BusinessParking\": \"{\\'garage\\': False, \\'street\\': False, \\'validated\\': False, \\'lot\\': True, \\'valet\\': False}\", \"Caters\": \"True\", \"DriveThru\": null, \"GoodForDancing\": \"False\", \"GoodForKids\": \"True\", \"GoodForMeal\": \"{\\'dessert\\': False, \\'latenight\\': False, \\'lunch\\': True, \\'dinner\\': True, \\'brunch\\': False, \\'breakfast\\': False}\", \"HasTV\": \"True\", \"Music\": \"{\\'dj\\': False, \\'background_music\\': False, \\'no_music\\': False, \\'jukebox\\': False, \\'live\\': False, \\'video\\': False, \\'karaoke\\': False}\", \"NoiseLevel\": \"u\\'average\\'\", \"RestaurantsAttire\": \"u\\'casual\\'\", \"RestaurantsDelivery\": \"None\", \"RestaurantsGoodForGroups\": \"True\", \"RestaurantsReservations\": \"True\", \"RestaurantsTableService\": \"True\", \"WheelchairAccessible\": \"True\", \"WiFi\": \"u\\'free\\'\"}', 'cuisine': '\"American\"', 'DogsAllowed': True, 'OutdoorSeating': True, 'borough': '\"Brooklyn\"', 'address': '{\"building\": \"69\", \"coord\": [-73.9757464, 40.687295], \"street\": \"Lafayette Avenue\", \"zipcode\": \"11217\"}', '_id': {'$oid': '6095a34a7c34416a90d322d1'}, 'name': '\"Academy Restauraunt\"', 'menu': '[\"Mozzarella sticks\", \"Cheeseburger\", \"Baked potato\", \"Breadsticks\", \"Caesar salad\", \"Chicken parmesan\", \"Pigs in a blanket\", \"Chicken soup\", \"Mac & cheese\", \"Mushroom swiss burger\", \"Spaghetti with meatballs\", \"Mashed potatoes\"]', 'TakeOut': 'true', 'PriceRange': '2.0', 'HappyHour': 'true', 'review_count': '173', 'sponsored': None, 'stars': 4.5}, hash='df7870b3103572b05e98091e4d4b52b238175eb08558831b621b6832c0472c2e'), <NodeRelationship.PREVIOUS: '2'>: RelatedNodeInfo(node_id='5fbb14fe-c8a8-4c4c-930d-2e07e4f77b47', node_type=<ObjectType.TEXT: '1'>, metadata={'restaurant_id': '40377111', 'attributes': '{\"Alcohol\": null, \"Ambience\": null, \"BYOB\": null, \"BestNights\": null, \"BikeParking\": \"True\", \"BusinessAcceptsBitcoin\": null, \"BusinessAcceptsCreditCards\": \"False\", \"BusinessParking\": \"{\\'garage\\': False, \\'street\\': True, \\'validated\\': False, \\'lot\\': False, \\'valet\\': False}\", \"Caters\": null, \"DriveThru\": \"True\", \"GoodForDancing\": null, \"GoodForKids\": null, \"GoodForMeal\": null, \"HasTV\": null, \"Music\": null, \"NoiseLevel\": null, \"RestaurantsAttire\": null, \"RestaurantsDelivery\": \"True\", \"RestaurantsGoodForGroups\": null, \"RestaurantsReservations\": null, \"RestaurantsTableService\": null, \"WheelchairAccessible\": null, \"WiFi\": null}', 'cuisine': '\"American\"', 'DogsAllowed': None, 'OutdoorSeating': None, 'borough': '\"Manhattan\"', 'address': '{\"building\": \"1207\", \"coord\": [-73.9592644, 40.8088612], \"street\": \"Amsterdam Avenue\", \"zipcode\": \"10027\"}', '_id': {'$oid': '6095a34a7c34416a90d321d6'}, 'name': '\"Amsterdam Restaurant & Tapas Lounge\"', 'menu': '[\"Green salad\", \"Cheddar Biscuits\", \"Lasagna\", \"Chicken parmesan\", \"Chicken soup\", \"Pigs in a blanket\", \"Caesar salad\", \"French fries\", \"Baked potato\", \"Mushroom swiss burger\", \"Grilled cheese sandwich\", \"Fried chicken\"]', 'TakeOut': 'true', 'PriceRange': '1.0', 'HappyHour': 'null', 'review_count': '6', 'sponsored': None, 'stars': 5.0}, hash='1261332dd67be495d0639f41b5f6462f87a41aabe20367502ef28074bf13e561'), <NodeRelationship.NEXT: '3'>: RelatedNodeInfo(node_id='10ad1a23-3237-4b68-808d-58fd7b7e5cb6', node_type=<ObjectType.TEXT: '1'>, metadata={}, hash='bc64dca2f9210693c3d5174aec305f25b68d080be65a0ae52f9a560f99992bb0')}, text='{\"restaurant_id\": \"40385767\", \"attributes\": \"{\\\\\"Alcohol\\\\\": \\\\\"u\\'beer_and_wine\\'\\\\\", \\\\\"Ambience\\\\\": \\\\\"{\\'touristy\\': False, \\'hipster\\': False, \\'romantic\\': False, \\'divey\\': False, \\'intimate\\': None, \\'trendy\\': None, \\'upscale\\': False, \\'classy\\': False, \\'casual\\': True}\\\\\", \\\\\"BYOB\\\\\": null, \\\\\"BestNights\\\\\": \\\\\"{\\'monday\\': False, \\'tuesday\\': False, \\'friday\\': True, \\'wednesday\\': False, \\'thursday\\': False, \\'sunday\\': False, \\'saturday\\': True}\\\\\", \\\\\"BikeParking\\\\\": \\\\\"True\\\\\", \\\\\"BusinessAcceptsBitcoin\\\\\": \\\\\"False\\\\\", \\\\\"BusinessAcceptsCreditCards\\\\\": \\\\\"True\\\\\", \\\\\"BusinessParking\\\\\": \\\\\"{\\'garage\\': False, \\'street\\': False, \\'validated\\': False, \\'lot\\': True, \\'valet\\': False}\\\\\", \\\\\"Caters\\\\\": \\\\\"True\\\\\", \\\\\"DriveThru\\\\\": null, \\\\\"GoodForDancing\\\\\": \\\\\"False\\\\\", \\\\\"GoodForKids\\\\\": \\\\\"True\\\\\", \\\\\"GoodForMeal\\\\\": \\\\\"{\\'dessert\\': False, \\'latenight\\': False, \\'lunch\\': True, \\'dinner\\': True, \\'brunch\\': False, \\'breakfast\\': False}\\\\\", \\\\\"HasTV\\\\\": \\\\\"True\\\\\", \\\\\"Music\\\\\": \\\\\"{\\'dj\\': False, \\'background_music\\': False, \\'no_music\\': False, \\'jukebox\\': False, \\'live\\': False, \\'video\\': False, \\'karaoke\\': False}\\\\\", \\\\\"NoiseLevel\\\\\": \\\\\"u\\'average\\'\\\\\", \\\\\"RestaurantsAttire\\\\\": \\\\\"u\\'casual\\'\\\\\", \\\\\"RestaurantsDelivery\\\\\": \\\\\"None\\\\\", \\\\\"RestaurantsGoodForGroups\\\\\": \\\\\"True\\\\\", \\\\\"RestaurantsReservations\\\\\": \\\\\"True\\\\\", \\\\\"RestaurantsTableService\\\\\": \\\\\"True\\\\\", \\\\\"WheelchairAccessible\\\\\": \\\\\"True\\\\\", \\\\\"WiFi\\\\\": \\\\\"u\\'free\\'\\\\\"}\", \"cuisine\": \"\\\\\"American\\\\\"\", \"DogsAllowed\": true, \"OutdoorSeating\": true, \"borough\": \"\\\\\"Brooklyn\\\\\"\",', start_char_idx=0, end_char_idx=1415, text_template='Metadata: {metadata_str}\\n-----\\nContent: {content}', metadata_template='{key}=>{value}', metadata_seperator='\\n'), score=0.7296431064605713),\n",
      " NodeWithScore(node=TextNode(id_='9cd153ba-2ab8-40aa-90f0-9da5ae24c632', embedding=None, metadata={'restaurant_id': '40392690', 'attributes': '{\"Alcohol\": \"u\\'full_bar\\'\", \"Ambience\": \"{\\'touristy\\': None, \\'hipster\\': True, \\'romantic\\': False, \\'divey\\': False, \\'intimate\\': None, \\'trendy\\': True, \\'upscale\\': None, \\'classy\\': True, \\'casual\\': True}\", \"BYOB\": \"False\", \"BestNights\": \"{\\'monday\\': False, \\'tuesday\\': False, \\'friday\\': True, \\'wednesday\\': False, \\'thursday\\': False, \\'sunday\\': False, \\'saturday\\': False}\", \"BikeParking\": \"True\", \"BusinessAcceptsBitcoin\": null, \"BusinessAcceptsCreditCards\": \"True\", \"BusinessParking\": \"{\\'garage\\': False, \\'street\\': True, \\'validated\\': False, \\'lot\\': False, \\'valet\\': False}\", \"Caters\": \"True\", \"DriveThru\": \"False\", \"GoodForDancing\": \"False\", \"GoodForKids\": \"True\", \"GoodForMeal\": \"{\\'dessert\\': None, \\'latenight\\': None, \\'lunch\\': True, \\'dinner\\': True, \\'brunch\\': False, \\'breakfast\\': False}\", \"HasTV\": \"False\", \"Music\": \"{\\'dj\\': False, \\'background_music\\': False, \\'no_music\\': False, \\'jukebox\\': False, \\'live\\': False, \\'video\\': False, \\'karaoke\\': False}\", \"NoiseLevel\": \"u\\'average\\'\", \"RestaurantsAttire\": \"\\'casual\\'\", \"RestaurantsDelivery\": \"True\", \"RestaurantsGoodForGroups\": \"True\", \"RestaurantsReservations\": \"False\", \"RestaurantsTableService\": \"True\", \"WheelchairAccessible\": \"True\", \"WiFi\": \"\\'free\\'\"}', 'cuisine': '\"Italian\"', 'DogsAllowed': True, 'OutdoorSeating': True, 'borough': '\"Manhattan\"', 'address': '{\"building\": \"11\", \"coord\": [-73.9828696, 40.7693649], \"street\": \"West   60 Street\", \"zipcode\": \"10023\"}', 'name': '\"Gabriel\\'S Bar & Grill\"', 'menu': '[\"Cheese Ravioli\", \"Neapolitan Pizza\", \"assorted gelato\", \"Vegetarian Baked Ziti\", \"Vegetarian Broccoli Pizza\", \"Lasagna\", \"Buca Trio Platter\", \"Spinach Ravioli\", \"Pasta with ricotta cheese\", \"Spaghetti\", \"Fried calimari\", \"Alfredo Pizza\"]', 'TakeOut': 'true', 'PriceRange': '2.0', 'HappyHour': 'true', 'review_count': '333', 'sponsored': None, 'stars': 4.0}, excluded_embed_metadata_keys=[], excluded_llm_metadata_keys=[], relationships={<NodeRelationship.SOURCE: '1'>: RelatedNodeInfo(node_id='77584933-8286-4277-bc56-bed76adcfd37', node_type=<ObjectType.DOCUMENT: '4'>, metadata={'restaurant_id': '40392690', 'attributes': '{\"Alcohol\": \"u\\'full_bar\\'\", \"Ambience\": \"{\\'touristy\\': None, \\'hipster\\': True, \\'romantic\\': False, \\'divey\\': False, \\'intimate\\': None, \\'trendy\\': True, \\'upscale\\': None, \\'classy\\': True, \\'casual\\': True}\", \"BYOB\": \"False\", \"BestNights\": \"{\\'monday\\': False, \\'tuesday\\': False, \\'friday\\': True, \\'wednesday\\': False, \\'thursday\\': False, \\'sunday\\': False, \\'saturday\\': False}\", \"BikeParking\": \"True\", \"BusinessAcceptsBitcoin\": null, \"BusinessAcceptsCreditCards\": \"True\", \"BusinessParking\": \"{\\'garage\\': False, \\'street\\': True, \\'validated\\': False, \\'lot\\': False, \\'valet\\': False}\", \"Caters\": \"True\", \"DriveThru\": \"False\", \"GoodForDancing\": \"False\", \"GoodForKids\": \"True\", \"GoodForMeal\": \"{\\'dessert\\': None, \\'latenight\\': None, \\'lunch\\': True, \\'dinner\\': True, \\'brunch\\': False, \\'breakfast\\': False}\", \"HasTV\": \"False\", \"Music\": \"{\\'dj\\': False, \\'background_music\\': False, \\'no_music\\': False, \\'jukebox\\': False, \\'live\\': False, \\'video\\': False, \\'karaoke\\': False}\", \"NoiseLevel\": \"u\\'average\\'\", \"RestaurantsAttire\": \"\\'casual\\'\", \"RestaurantsDelivery\": \"True\", \"RestaurantsGoodForGroups\": \"True\", \"RestaurantsReservations\": \"False\", \"RestaurantsTableService\": \"True\", \"WheelchairAccessible\": \"True\", \"WiFi\": \"\\'free\\'\"}', 'cuisine': '\"Italian\"', 'DogsAllowed': True, 'OutdoorSeating': True, 'borough': '\"Manhattan\"', 'address': '{\"building\": \"11\", \"coord\": [-73.9828696, 40.7693649], \"street\": \"West   60 Street\", \"zipcode\": \"10023\"}', '_id': {'$oid': '6095a34b7c34416a90d3243a'}, 'name': '\"Gabriel\\'S Bar & Grill\"', 'menu': '[\"Cheese Ravioli\", \"Neapolitan Pizza\", \"assorted gelato\", \"Vegetarian Baked Ziti\", \"Vegetarian Broccoli Pizza\", \"Lasagna\", \"Buca Trio Platter\", \"Spinach Ravioli\", \"Pasta with ricotta cheese\", \"Spaghetti\", \"Fried calimari\", \"Alfredo Pizza\"]', 'TakeOut': 'true', 'PriceRange': '2.0', 'HappyHour': 'true', 'review_count': '333', 'sponsored': None, 'stars': 4.0}, hash='c4dcc57a697cd2fe3047a280573c0f54bc5236e1d5af2228737af77613c9dbf7'), <NodeRelationship.PREVIOUS: '2'>: RelatedNodeInfo(node_id='6e1ead27-3679-48fb-b160-b47db523a3ce', node_type=<ObjectType.TEXT: '1'>, metadata={'restaurant_id': '40392496', 'attributes': '{\"Alcohol\": \"u\\'none\\'\", \"Ambience\": \"{\\'touristy\\': False, \\'hipster\\': False, \\'romantic\\': False, \\'intimate\\': None, \\'trendy\\': False, \\'upscale\\': False, \\'classy\\': False, \\'casual\\': True}\", \"BYOB\": null, \"BestNights\": null, \"BikeParking\": \"True\", \"BusinessAcceptsBitcoin\": null, \"BusinessAcceptsCreditCards\": null, \"BusinessParking\": \"{\\'garage\\': False, \\'street\\': True, \\'validated\\': False, \\'lot\\': False, \\'valet\\': False}\", \"Caters\": \"False\", \"DriveThru\": null, \"GoodForDancing\": null, \"GoodForKids\": \"True\", \"GoodForMeal\": \"{\\'dessert\\': False, \\'latenight\\': False, \\'lunch\\': True, \\'dinner\\': True, \\'brunch\\': None, \\'breakfast\\': False}\", \"HasTV\": \"True\", \"Music\": null, \"NoiseLevel\": \"u\\'average\\'\", \"RestaurantsAttire\": \"u\\'casual\\'\", \"RestaurantsDelivery\": \"True\", \"RestaurantsGoodForGroups\": \"False\", \"RestaurantsReservations\": \"False\", \"RestaurantsTableService\": \"True\", \"WheelchairAccessible\": null, \"WiFi\": \"\\'free\\'\"}', 'cuisine': '\"English\"', 'DogsAllowed': True, 'OutdoorSeating': True, 'borough': '\"Manhattan\"', 'address': '{\"building\": \"253\", \"coord\": [-74.0034571, 40.736351], \"street\": \"West   11 Street\", \"zipcode\": \"10014\"}', '_id': {'$oid': '6095a34b7c34416a90d32435'}, 'name': '\"Tartine\"', 'menu': 'null', 'TakeOut': 'true', 'PriceRange': '2.0', 'HappyHour': 'true', 'review_count': '436', 'sponsored': None, 'stars': 4.5}, hash='146bffad5c816926ec1008d966caab7c0df675251ccca5de860f8a2160bb7a34'), <NodeRelationship.NEXT: '3'>: RelatedNodeInfo(node_id='6640911b-3d8e-4bad-a016-4c3d91444b0c', node_type=<ObjectType.TEXT: '1'>, metadata={}, hash='39984a7534d6755344f0887e0d6a200eaab562a7dc492afe292040c0022282bd')}, text='{\"restaurant_id\": \"40392690\", \"attributes\": \"{\\\\\"Alcohol\\\\\": \\\\\"u\\'full_bar\\'\\\\\", \\\\\"Ambience\\\\\": \\\\\"{\\'touristy\\': None, \\'hipster\\': True, \\'romantic\\': False, \\'divey\\': False, \\'intimate\\': None, \\'trendy\\': True, \\'upscale\\': None, \\'classy\\': True, \\'casual\\': True}\\\\\", \\\\\"BYOB\\\\\": \\\\\"False\\\\\", \\\\\"BestNights\\\\\": \\\\\"{\\'monday\\': False, \\'tuesday\\': False, \\'friday\\': True, \\'wednesday\\': False, \\'thursday\\': False, \\'sunday\\': False, \\'saturday\\': False}\\\\\", \\\\\"BikeParking\\\\\": \\\\\"True\\\\\", \\\\\"BusinessAcceptsBitcoin\\\\\": null, \\\\\"BusinessAcceptsCreditCards\\\\\": \\\\\"True\\\\\", \\\\\"BusinessParking\\\\\": \\\\\"{\\'garage\\': False, \\'street\\': True, \\'validated\\': False, \\'lot\\': False, \\'valet\\': False}\\\\\", \\\\\"Caters\\\\\": \\\\\"True\\\\\", \\\\\"DriveThru\\\\\": \\\\\"False\\\\\", \\\\\"GoodForDancing\\\\\": \\\\\"False\\\\\", \\\\\"GoodForKids\\\\\": \\\\\"True\\\\\", \\\\\"GoodForMeal\\\\\": \\\\\"{\\'dessert\\': None, \\'latenight\\': None, \\'lunch\\': True, \\'dinner\\': True, \\'brunch\\': False, \\'breakfast\\': False}\\\\\", \\\\\"HasTV\\\\\": \\\\\"False\\\\\", \\\\\"Music\\\\\": \\\\\"{\\'dj\\': False, \\'background_music\\': False, \\'no_music\\': False, \\'jukebox\\': False, \\'live\\': False, \\'video\\': False, \\'karaoke\\': False}\\\\\", \\\\\"NoiseLevel\\\\\": \\\\\"u\\'average\\'\\\\\", \\\\\"RestaurantsAttire\\\\\": \\\\\"\\'casual\\'\\\\\", \\\\\"RestaurantsDelivery\\\\\": \\\\\"True\\\\\", \\\\\"RestaurantsGoodForGroups\\\\\": \\\\\"True\\\\\", \\\\\"RestaurantsReservations\\\\\": \\\\\"False\\\\\", \\\\\"RestaurantsTableService\\\\\": \\\\\"True\\\\\", \\\\\"WheelchairAccessible\\\\\": \\\\\"True\\\\\", \\\\\"WiFi\\\\\": \\\\\"\\'free\\'\\\\\"}\", \"cuisine\": \"\\\\\"Italian\\\\\"\", \"DogsAllowed\": true, \"OutdoorSeating\": true,', start_char_idx=0, end_char_idx=1382, text_template='Metadata: {metadata_str}\\n-----\\nContent: {content}', metadata_template='{key}=>{value}', metadata_seperator='\\n'), score=0.7284677028656006)]\n"
     ]
    }
   ],
   "source": [
    "import pprint\n",
    "from llama_index.core.response.notebook_utils import display_response\n",
    "\n",
    "query_engine = index.as_query_engine()\n",
    "\n",
    "query = \"search query: Anything that doesn't have alcohol in it\"\n",
    "\n",
    "response = query_engine.query(query)\n",
    "display_response(response)\n",
    "pprint.pprint(response.source_nodes)"
   ]
  }
 ],
 "metadata": {
  "colab": {
   "provenance": []
  },
  "kernelspec": {
   "display_name": "Python 3",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
